package kr.co.bettersoftportal.site.management.service;


import kr.co.bettersoftportal.site.domain.CommunityBoard;
import kr.co.bettersoftportal.site.domain.UploadFile;
import kr.co.bettersoftportal.site.management.dao.CommunityBoardDao;
import kr.co.bettersoftportal.site.management.dao.MemberDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

public class MovieBoardServiceImpl implements CommunityBoardService{


    @Autowired
    private MemberDao memberDao = null;

    @Autowired
    private CommunityBoardDao communityBoardDao = null;

    public List getBoardListBySearchword(CommunityBoard communityBoard) {
        return communityBoardDao.getBoardListBySearchword(communityBoard);
    }

    public List getBoardListByNo(CommunityBoard communityBoard){
        return communityBoardDao.getBoardListByNo(communityBoard);
    }

    public String getTotalCountBySearchword(CommunityBoard communityBoard) {
        return communityBoardDao.getTotalCountBySearchword(communityBoard);
    }

    public String getTotalCountByNo(CommunityBoard communityBoard) {
        return communityBoardDao.getTotalCountBySearchword(communityBoard);
    }

    public String writeBoard(CommunityBoard board){
        return communityBoardDao.writeBoard(board);
    }

    public String updateBoard(CommunityBoard board){
        return communityBoardDao.updateBoard(board);
    }

    public String deleteBoard(CommunityBoard board){
        return communityBoardDao.deleteBoard(board);
    }

    public CommunityBoard getBoardByNo(String community_board_no){
        return communityBoardDao.getBoardByNo(community_board_no);
    }

    public String updateReplyCount(CommunityBoard board){
        return communityBoardDao.updateReplyCount(board);
    }

    public String updateHitCount(CommunityBoard board){
        return communityBoardDao.updateHitCount(board);
    }

    public String updateVoteCount(CommunityBoard board){
        return communityBoardDao.updateVoteCount(board);
    }

    public String updateNegativeCount(CommunityBoard board){
        return communityBoardDao.updateNegativeCount(board);
    }

    public String updateReportCount(CommunityBoard board){
        return communityBoardDao.updateReportCount(board);
    }

    /**
     * 파일 업로드 서비스
     * @param file
     * @param realPath
     * @return
     */
    public UploadFile uploadFile(MultipartFile file, String realPath){

        InputStream stream; // Input Stream 생성

        UUID uuid = UUID.randomUUID(); // 랜덤 UUID 이름 생성
        String tempFileName = uuid.toString(); // 임시 파일 이름 저장

        try {
            stream = file.getInputStream();

            OutputStream bos = new FileOutputStream(realPath + tempFileName);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            stream.close();

            // 파일 쓰고 stream 닫기

            /*
            if (logger.isDebugEnabled()) {
                logger.debug("The file has been written to \"" + realPath
                        + tempFileName);
            }
            */

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        UploadFile boardFile = new UploadFile();
        
        boardFile.setFileName(file.getOriginalFilename());
        boardFile.setFileSize(Long.toString(file.getSize()));
        boardFile.setContentType(file.getContentType());
        boardFile.setTempFileName(tempFileName);

        return boardFile;
    }

    /**
     * 추천 중복 체크
     * @param vote
     * @return
     */
//    public String checkDuplicateVote(Vote vote) {
//        return communityBoardDao.checkDuplicateVote(vote);
//    }
//
//    public String insertVoterToList(Vote vote) {
//        return communityBoardDao.insertVoterToList(vote);
//    }

    /**
     * 게시글 신고 하기
     * @param report
     * @return
     */
//    public String communityBoardReport(Report report) {
//        return communityBoardDao.communityBoardReport(report);
//    }

    /**
     * 게시글 신고 전 중복 체크.
     * @param
     * @return
     */

//    public String checkDuplicateReport(Report report) {
//        return communityBoardDao.checkDuplicateReport(report);
//    }

    public String updateBoardNotImage(CommunityBoard board) {
        return communityBoardDao.updateBoardNotImage(board);
    }


    ///////////////////////////////////////
	// Spring Framework Setter Injection //
	///////////////////////////////////////

}
